#include "question.h"

/*!
 * /param score How much times should be this question asked.
 */
Question::Question(QObject* parent): QObject(parent) {
}

/*!
 * Function shuffles answers, making it easier to display.
 */
void Question::shuffleAnswers() {
    for (int i = answers.size()-1;i>1;i--) {
        int j = qrand()%i;
        answers.swap(i,j);
        trueAnswers.swap(i,j);
    }
}

/*!
  Function that returns ID of first right answer.
  @param index Index of the answer from which we should look for the right one.
    0 to look from the beginning.
  @deprecated Question::isCorrect should be used in loop instead, as it allows
    multiple correct answers.
  @return int id of right answer, or -1 if there's none.
 */
int Question::getRightAnswerId(int index) {
    int result = -1;
    for (int i=index;i<trueAnswers.size();i++) {
        if (trueAnswers[i]) {
            result = i;
            break;
        }
    }
    return result;
}

/*!
  Function checks if answer with given ansId is correct.
  @param ansId ID of a selected answer.
  @return True if selected answer is correct.
 */
bool Question::isCorrect(int ansId) {
    return this->trueAnswers[ansId];
}

/*!
  Performs an action depending on corectness of an answer.
  Usually increases or decreases the score.
  @param goodAnswer True indicates that user chose good answer,
    otherwise function assumes that it was bad.
 */
void Question::selectedAnswer(bool goodAnswer) {
    if (goodAnswer) {
        this->score--;
    } else {
        this->score += this->penalty;
    }
}

void Question::setScore(int score) {
    // TODO: doesn't work ATM
    this->score = score;
}

void Question::setPenalty(int penalty) {
    // TODO: doesn't work ATM
    this->penalty = penalty;
}
